package com.csu.analysis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.csu.analysis.service.MattressService;
import com.csu.device.entity.AlarmDO;
import com.csu.device.entity.Device;
import com.csu.device.service.HardWareService;
import com.csu.push.WebSocketPush;
import com.csu.util.DeviceUtil;
import com.csu.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Service
public class MattressServiceImpl implements MattressService {


    @Autowired
    private HardWareService hardWareService;

    @Override
    public void proxyMattress(JSONObject jsonObject) {
        String deviceSn = jsonObject.getString("sn");

        Device device = (Device) RedisUtil.get(deviceSn);
        if (null == device) {
            return;
        }

        WebSocketPush.mattressPush(device.getDeviceRelas(),jsonObject);
        JSONObject jsonData = jsonObject.getJSONObject("d");

        //判断是否在晚上12点到早上5点之间离床
        Boolean st = jsonData.getString("st").equals("off")?true:false;
        this.alarm(device,st,jsonObject);
        //[{"timeCreate":"2019-07-24T16:47:06.5583824+08:00","mo":"18","ma":"HT",
        // "d":{"st":"off","hb":0,"br":0,"wt":false,"od":0,"we":1,
        // "p":[0,0],"fv":13},"v":2,"sn":"Z56307"}]


        //wt==1 尿湿 wt==0非尿湿
        boolean wt = jsonData.getBoolean("wt");
        this.wetness(wt,device,jsonObject);

    }

    /**
     * @Author: lds
     * @Description: 判断是否在晚上12点到早上5点之间离床，没回来10分钟报一次，30报一次，1小时报一次
     * @Date: Created in 9:47 2019/11/6
     * @param [device, st, jsonObject]  
     * @return void  
     */
    private void alarm(Device device, Boolean st, JSONObject jsonObject) {
        //离床
        if(st){
            //判断是否在晚上12点到早上5点之间
            if(DeviceUtil.timeBettow()){
                //判断redis是否有数据，有，判断是否到10钟，没有存入
                AlarmDO alarmDO = (AlarmDO)RedisUtil.get("alarm:st:" + device.getDeviceSn());
                if(alarmDO==null){
                    alarmDO=new AlarmDO();
                    alarmDO.setDeviceSn(device.getDeviceSn());
                    alarmDO.setStTime(new Date());
                    alarmDO.setAlarmLevel(0);
                    RedisUtil.set("alarm:st:"+device.getDeviceSn(), alarmDO);
                }else {
                    long time = alarmDO.getStTime().getTime();
                    Map<String,Object> map = DeviceUtil.getLevel(time);
                    int level = Integer.valueOf(map.get("level")+"");
                    int alarmLevel = alarmDO.getAlarmLevel();
                    if(level>alarmLevel){
                            hardWareService.callPolice(level,String.valueOf(map.get("remark")),device,3);
                            alarmDO.setAlarmLevel(level);
                            RedisUtil.set("alarm:st:"+device.getDeviceSn(), alarmDO);
                    }
                }

            }else {
                RedisUtil.del("alarm:st:" + device.getDeviceSn());
            }
        }else {
            //回来，清除redis数据
            RedisUtil.del("alarm:st:" + device.getDeviceSn());
        }

    }


    /**
     * 心率和心跳忽高忽低监测
     * 床垫3秒传一次
     * 监测为逻辑为 没30秒监测一次 获取心率和心态的波动率
     *
     * @param deviceSn
     * @param jsonObject
     */
    private void heartbeat(String deviceSn,  JSONObject jsonObject) {
        //todo 需要通过波动率算法来实现

    }


    /**
     * 尿湿处理
     * 有尿湿就记录
     * wt==1 尿湿
     * wt==0 非尿湿
     * @param wt
     * @param device
     */
    private void wetness(boolean wt, Device device,JSONObject jsonObject) {
        if (wt) {
            hardWareService.callPolice(3,"尿湿报警",device,3);
        }
    }
}
